# -*- coding: utf-8 -*-
import scrapy
import datetime
from ..items import QvanshuwangItem, ContentItem
from scrapy_redis.spiders import RedisSpider

class QsnovelSpider(scrapy.Spider):
    name = 'qsnovel'
    # allowed_domains = ['dd']
    start_urls = ['http://www.quanshuwang.com/list/2_1.html']

    def parse(self, response):
        novel_urls = response.xpath('//ul[@class="seeWell cf"]/li/a/@href').extract()
        for novel_url in novel_urls[:1]:
            yield scrapy.Request(url=novel_url, callback=self.get_info)

    def get_info(self, response):
        item = QvanshuwangItem()
        category = response.xpath('//meta[@property="og:novel:category"]/@content').extract()[0]
        author = response.xpath('//meta[@property="og:novel:author"]/@content').extract()[0]
        book_name = response.xpath('//meta[@property="og:novel:book_name"]/@content').extract()[0]
        status = response.xpath('//meta[@property="og:novel:status"]/@content').extract()[0]
        des = response.xpath('//meta[@property="og:description"]/@content').extract()[0]
        des = des.split('<br />')  # 分割字符串
        description = "".join([i.strip() for i in des])  # 拼接
        c_time = datetime.datetime.now()
        url = response.url
        print(category, author, book_name, status, description, c_time, url)
        item['category'] = category
        item['author'] = author
        item['book_name'] = book_name
        item['status'] = status
        item['description'] = description
        item['c_time'] = c_time
        item['url'] = url
        yield item
        chapter_url = response.xpath('//a[@class="reader"]/@href').extract()[0]
        yield scrapy.Request(chapter_url, callback=self.get_chapter_info, meta={"novel": item})

    def get_chapter_info(self, response):
        novel_item = response.meta["novel"]
        chapter = response.xpath('//div[@class="clearfix dirconone"]/li/a')
        chapter_list = []
        for a in chapter[:2]:
            chapter_url = a.xpath('./@href').extract()[0]
            yield scrapy.Request(chapter_url, callback=self.get_content)
            title = a.xpath('./text()').extract()[0]
            chapter_list.append((title, chapter_url))
        novel_item["chapter_list"] = chapter_list
        yield novel_item

    def get_content(self, response):
        item = ContentItem()
        con = response.xpath('//div[@id="content"]/text()').extract()
        content = "".join([i.strip() for i in con])
        item["content"] = content
        item["url"] = response.url
        yield item
